import formatDate from '@/utils/format-date'
import storage from '@/utils/storage'

export default {
  namespaced: true,
  state: {
    lists: [],
    listsMap: {}
  },
  getters: {
    hasList (state) {
      return state.lists.length > 0
    }
  },
  mutations: {
    updateLists (state, lists) {
      state.lists = lists
    },
    updateListsMap (state, listData) {
      state.listsMap[listData.id] = listData
    },
    createList (state, listData) {
      state.lists.unshift(listData)
      state.listsMap[listData.id] = listData
    },
    changeEditing (state, listData) {
      const { index, editItem } = listData
      state.lists[index].isEditing = !state.lists[index].isEditing
      if (editItem) {
        state.lists[index].todo = editItem
      }
    },
    deleteItem (state, index) {
      state.lists.splice(index, 1)
    },
    changecheck (state, index) {
      state.lists[index].checked = !state.lists[index].checked
    },
    changeIndex (state, item) {
      const { dragIndex, targetIndex } = item
      const dragItem = JSON.parse(JSON.stringify(state.lists[dragIndex]))
      state.lists.splice(dragIndex, 1)
      state.lists.splice(targetIndex, 0, dragItem)
    }
  },
  actions: {
    changeListchecked ({ commit, state }, listData) {
      const index = state.lists.findIndex(item => item.id === listData.id)
      if (index === -1) {
        return false
      }
      commit('changecheck', index)
      storage.set('lists', JSON.stringify(state.lists))
      return true
    },
    deleteListItem ({ commit, state }, listData) {
      const index = state.lists.findIndex(item => item.id === listData.id)
      if (index === -1) {
        return false
      }
      commit('deleteItem', index)
      storage.set('lists', JSON.stringify(state.lists))
      return true
    },
    getList ({ commit }) {
      const lists = storage.get('lists')
      if (lists) {
        commit('updateLists', JSON.parse(lists))
      }
    },
    changeListEditing ({ state, commit }, listData) {
      const index = state.lists.findIndex(item => item.id === listData.id)
      if (index === -1) {
        return false
      }
      commit('changeEditing', { ...listData, index })
      commit('updateListsMap', { listData })
      storage.set('lists', JSON.stringify(state.lists))
      return true
    },
    changeListIndex ({ state, commit }, item) {
      commit('changeIndex', item)
      storage.set('lists', JSON.stringify(state.lists))
    },
    async updateListData ({ state, commit }, listData) {
      const { listsMap, lists } = state
      // 表单的创建 && 更新
      const params = {
        todo: listData.todo || '',
        started_at: listData.startedAt || formatDate(new Date()),
        updated_at: listData.updateAt || formatDate(new Date()),
        id: listData.id || Date.now().toString(36),
        isEditing: false,
        checked: false
      }

      const index = lists.findIndex(item => item.todo === listData.todo)
      if (index !== -1) {
        return false
      }
      // 更新
      if (listData.todo && listData.id) {
        delete listsMap[listData.id]
        commit('updateListsMap', state, ...listData)
      }
      // 创建
      commit('createList', {
        ...listData,
        ...params
      })
      storage.set('lists', JSON.stringify(lists))
      return true
    }
  }
}
